package com.inool.controller;

import com.inool.model.Student;
import com.inool.service.StudentService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inool.common.result.Result;

import java.util.Date;
import java.util.List;

@Tag(name = "学生管理")
@RestController
@RequestMapping("/api/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @PostMapping("add")
    @Operation(summary = "添加学生")
    public Result<Student> addStudent(@RequestBody Student student) {
        Student newStudent = studentService.addStudent(student);
        return Result.ok(newStudent);
    }

    @Operation(summary = "更新学生")
    @PostMapping("update/{id}")
    public Result<Student> updateStudent(@PathVariable String id, @RequestBody Student student) {
        Student updatedStudent = studentService.updateStudent(id, student);
        if (updatedStudent != null) {
            return Result.ok(updatedStudent);
        }
        return Result.ok();
    }

    @Operation(summary = "删除学生")
    @PostMapping("remove/{id}")
    public Result<Void> deleteStudent(@PathVariable String id) {
        studentService.deleteStudent(id);
        return Result.ok();
    }

    @Operation(summary = "根据学生ID获取学生信息")
    @GetMapping("get-student/{id}")
    public Result<Student> getStudentById(@PathVariable String id) {
        Student student = studentService.getStudentById(id);
        if (student != null) {
            return Result.ok(student);
        }
        return Result.ok();
    }

    @Operation(summary = "获取全部学生信息")
    @GetMapping("get-all")
    public Result<List<Student>> getAllStudents() {
        List<Student> students = studentService.getAllStudents();
        return Result.ok(students);
    }

    @Operation(summary = "分页获取全部学生信息")
    @GetMapping("page")
    public Result<Page<Student>> pageStudent(@RequestParam long current, @RequestParam long size,
                                             String studentNumber, String name, String title, Date startTime, Date endTime) {
        Page<Student> page = studentService.pageStudent(current, size, studentNumber, name, title, startTime, endTime);
        return Result.ok(page);
    }
}    